namespace System.IO
{
    /// <summary>
    /// System.IO.File扩展
    /// </summary>
    public static class FileExtension
    {
        /// <summary>
        /// 将文件转为Base64字符串
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static string ToBase64String(string filePath)
        {
            byte[] fileBytes = File.ReadAllBytes(filePath);
            //模拟网络传输的字符串编码
            return Convert.ToBase64String(fileBytes).Replace("+", "%2b");
        }

        /// <summary>
        /// 将Base64字符串转为文件
        /// </summary>
        /// <param name="base64String">base64字符串</param>
        /// <param name="filePath">保存路径</param>
        public static void FromBase64String(string base64String, string filePath)
        {
            var newFileBytes = Convert.FromBase64String(base64String.Replace("%2b", "+"));
            File.WriteAllBytes(filePath, newFileBytes);
        }
    }
}
